import pandas as pd


def write_obstacle_results_to_excel(data, excel_filename="revit_result.xlsx"):
    def find_obstacle_success_subarrays(data):
        obstacle_success_arrays = []
        obstacle_fail_arrays = []

        for subarray1 in data:
            if isinstance(subarray1, list):  # Check if it's a valid subarray
                for subarray2 in subarray1:
                    if isinstance(subarray2, list):  # Check if it's a valid subarray
                        status = subarray2[-1]
                        if status == '避障成功':
                            obstacle_success_arrays.append(subarray2)
                        elif status == '避障失败':
                            obstacle_fail_arrays.append(subarray2)

        return obstacle_success_arrays, obstacle_fail_arrays

    success_arrays, fail_arrays = find_obstacle_success_subarrays(data)
    success_df = pd.DataFrame(success_arrays,
                              columns=['Start Point', 'End Point', 'Width', 'Height', 'Slope', 'Status'])

    fail_df = pd.DataFrame(fail_arrays, columns=['Start Point', 'End Point', 'Width', 'Height', 'Slope', 'Status'])

    # 将数据写入Excel文件
    with pd.ExcelWriter(excel_filename, engine='xlsxwriter') as writer:
        success_df.to_excel(writer, sheet_name='避障成功', index=False)
        fail_df.to_excel(writer, sheet_name='避障失败', index=False)